<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Modelsphp
 *
 * @author C_NGA
 */
include_once '../config.php';
class Modelsphp {
    //put your code here
    public $db;
    public function __construct() {
       $this->db=  mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('Không kết nối được CSDL');
        mysqli_set_charset($this->db, "utf8");
       
       }
    public function getAllUser()
    {
         $sql = "SELECT * FROM `tbd_user`";
        return mysqli_query($this->db, $sql) ;
    }
    public function getUserByName($name,$password)
    {
        $password=md5($password);
        $sql = "SELECT * FROM `tbd_user` where Username='$name' and Password='$password'";
        return mysqli_query($this->db, $sql);
    }
    public function getUserById($id)
    {
      //  $password=md5($password);
        $sql = "SELECT * FROM `tbd_user` where Id='$id'";
        return mysqli_query($this->db, $sql);
    }
    //INSERT INTO `tbd_user`(`Id`, `Name`, `UserName`, `Password`, `Level`, `Admin`, `Ord`, `Active`, `EndAccess`) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6],[value-7],[value-8],[value-9])
    public function addUser($name,$username,$password,$check_add)
    {
        echo "<h1>$check_add</h1>";
        $endaccess=date("Y-m-d");
        $password=  md5($password);
    // $sql = "INSERT INTO `tbd_user`(`Name`, `UserName`, `Password`, `Level`, `Admin`, `Ord`, `Active`) VALUES (\'mrquang\',\'mrquangson\',\'1234\',\'1\',\'1\',\'2\',\'1\')";
       // $sql="INSERT INTO `tbd_user`(`Name`, `UserName`,'Password',`EndAccess`,'Active') VALUES ('$name','$username','$password','$endaccess')";
        $sql = "INSERT INTO tbd_user(Name, UserName, Password, EndAccess, Active) VALUES "
                . "('$name','$username','$password','$endaccess', '$check_add')";
        mysqli_query($this->db, $sql) or die(mysqli_errno($this->db))  ;
        
       // return mysqli_query($this->db, $sql);
    }
       public function deleteUser($Id)
    {
       
           $sql = "DELETE FROM `tbd_user` WHERE Id=$Id";
        mysqli_query($this->db, $sql) or die(mysqli_errno($this->db))  ;
        
       // return mysqli_query($this->db, $sql);
    }
        public function updateUser($id,$name,$username,$password,$check_add)
    {
            $password=  md5($password);
           $sql = "UPDATE `tbd_user` SET `Name`='$name',`UserName`='$username',`Password`='$password',`Active`='$check_add' WHERE Id='$id'";
        mysqli_query($this->db, $sql)  ;
        
       // return mysqli_query($this->db, $sql);
    }
      public function insertCate($parent_id,$title,$note,$description)
    {
       $created_user_id=1;
       $created_time=  date("Y-m-d");
       $modified_user_id=1;
       $modified_time=date("Y-m-d");
//       $sql = "INSERT INTO `categories`( `parent_id`, `name_cate`, `note`, `description`, `created_user_id`, `created_time`, `modified_user_id`, `modified_time`)"
//       ." VALUES ($parent_id,'$title,$note','$description',1,now(),1,now())";
           $sql = "INSERT INTO `categories`( `parent_id`, `name_cate`,  `note`, `description`, `created_user_id`, `created_time`, `modified_user_id`, `modified_time`) "
             ."  VALUES ('$parent_id','$title','$note','$description','$created_user_id','$created_time','$modified_user_id','$modified_time')";
   return mysqli_query($this->db, $sql);
        
       // return mysqli_query($this->db, $sql);
    }
        public function getCateParent()
    {
       
          $sql = "select * from categories where parent_id=0";
      return  mysqli_query($this->db, $sql)   ;
        
       // return mysqli_query($this->db, $sql);
    }
	  public function getCateParentCheckin()
    {
       
          $sql = "select * from categories where parent_id=0 and Active=1";
      return  mysqli_query($this->db, $sql)   ;
        
       // return mysqli_query($this->db, $sql);
    }
    public function getContentByCate($id)
    {
       
          $sql = "select * from content where catid=$id";
      return  mysqli_query($this->db, $sql)   ;
        
       // return mysqli_query($this->db, $sql);
    }
      public function getAllCate()
    {
         $sql = "SELECT * FROM `categories`";
        return mysqli_query($this->db, $sql) ;
    }
      public function deleteCate($Id)
    {
       
           $sql = "DELETE FROM `categories` WHERE Id=$Id";
        mysqli_query($this->db, $sql)  ;
        
       // return mysqli_query($this->db, $sql);
    }
	   public function deleteArt($Id)
    {
       
           $sql = "DELETE FROM content WHERE Id=$Id";
        mysqli_query($this->db, $sql)  ;
        
       // return mysqli_query($this->db, $sql);
    }
    public function getArticleById($id)
    {
         $sql = "select * from content where id='$id'";
                       return mysqli_query($this->db, $sql)  ;
    }
	 public function getContentPresentById()
    {
         $sql = "select * from present where id=1";
                       return mysqli_query($this->db, $sql)  ;
    }

    public function updateArticle($id,$title,$introtext,$fulltext,$catid,$create_by,$modified_by,$images,$metakey,$active,$checkin)
    {
       $create=date("Y-m-d");
       $modified=date("Y-m-d");
           $sql = "UPDATE `content` SET `title`=`$title`,`introtext`=`$introtext`,`fulltext`=`fulltext`,`catid`=`$catid`,`created`='$create',`created_by`=`$create_by`,`modified_by`=`$modified_by`,`images`=`$images`,`metakey`=`$metakey`,`Active`=`$active`,`checkin`=`,$checkin` WHERE id=$id";
		return   mysqli_query($this->db, $sql)  or die(mysqli_errno($this->db))  ;
        
       // return mysqli_query($this->db, $sql);
    }
	public function insertArticle($title,$introtext,$fulltext,$cateid,$create_by,$modified_by,$publish_up,$images,$metakey)
    {
       $create=date("Y-m-d");
       $modified=date("Y-m-d");
           $sql = "INSERT INTO content(title , introtext , fulltext , catid , created , created_by , modified , modified_by , publish_up , images , metakey )"
                               ." VALUES ($title,$introtext,$fulltext,$cateid,$create,$create_by,$modified,$modified_by,$publish_up,$images,$metakey)";
        mysqli_query($this->db, $sql)  ;
        
       // return mysqli_query($this->db, $sql);
    }
    public function getCart($str) {
        $sql="select * from content where id in ($str)";
        return mysqli_query($this->db, $sql)  ;
    }
	public function updateActive($id,$cmd)
	{
		$sql="UPDATE tbd_user SET  Active='$cmd' where Id='$id'";
		mysqli_query($this->db, $sql)  ;
	}
	public function updateActiveArt($id,$cmd)
	{
		$sql="UPDATE content SET  Active='$cmd' where Id='$id'";
		mysqli_query($this->db, $sql)  ;
	}
	public function updateCheckinArt($id,$cmd)
	{
		$sql="UPDATE content SET  checkin='$cmd' where Id='$id'";
		mysqli_query($this->db, $sql)  ;
	}
	public function updateActiveCate($id,$cmd)
	{
		$sql="UPDATE categories SET  Active='$cmd' where Id='$id'";
		mysqli_query($this->db, $sql)  ;
	}
	public function getlistArticle($cateid=0,$str="")	
	{
		$sql="select * from content";
		if($cateid==0)
		{
			if($str=="")
				$sql="select * from content";
			else
				$sql="select * from content where introtext like '$str' or title like '$str'";
				
		}
		else
		{
			if($str=="")
				$sql="select * from content where cateid='$str'";
			else
				$sql="select * from content where  cateid='$str' and (introtext like '$str' or title like '$str')";
		}
		
		
		return mysqli_query($this->db, $sql)  ;
		
		
	}
	public function getCheckin()
	{
		$sql="select * from content,categories where content.catid=categories.id and content.Active=1 and (content.checkin=1) and categories.active =1";
		
		 return mysqli_query($this->db, $sql)  ;
	}
	
}
